process FCSGX_RUNGX {
    tag "$meta.id"
    label 'process_high'

    conda "${moduleDir}/environment.yml"
    container "${ workflow.containerEngine == 'singularity' && !task.ext.singularity_pull_docker_container ?
        'https://depot.galaxyproject.org/singularity/ncbi-fcs-gx:0.5.5--h9948957_0':
        'biocontainers/ncbi-fcs-gx:0.5.5--h9948957_0' }"

    input:
    tuple val(meta), val(taxid), path(fasta)
    path gxdb
    val ramdisk_path

    output:
    tuple val(meta), path("*.fcs_gx_report.txt"), emit: fcsgx_report
    tuple val(meta), path("*.taxonomy.rpt")     , emit: taxonomy_report
    tuple val(meta), path("*.summary.txt")      , emit: log
    tuple val(meta), path("*.hits.tsv.gz")      , emit: hits, optional: true
    path "versions.yml"                         , emit: versions

    when:
    task.ext.when == null || task.ext.when

    script:
    def args = task.ext.args ?: ''
    def prefix = task.ext.prefix ?: "${meta.id}"
    def database = ramdisk_path ?: gxdb
    ( ramdisk_path ?
    """
    if [ -d "${database}" ]; then
        echo "ERROR: Database exists in memory, and may be in use by another process" >&2
        ls -l ${database}
        exit 1
    fi
    # Clean up shared memory on exit
    trap "rm -rf ${database}" EXIT
    # Copy DB to RAM-disk when supplied. Otherwise, rungx is very slow.
    rclone copy ${gxdb} ${database}

    """: "")
    <<
    """
    export GX_NUM_CORES=${task.cpus}
    run_gx.py \\
        --fasta ${fasta} \\
        --gx-db ${database} \\
        --tax-id ${taxid} \\
        --generate-logfile true \\
        --out-basename ${prefix} \\
        --out-dir . \\
        ${args}

    cat <<-END_VERSIONS > versions.yml
    "${task.process}":
        fcsgx: \$( gx --help | sed '/build/!d; s/.*:v//; s/-.*//' )
    END_VERSIONS
    """

    stub:
    // def args = task.ext.args ?: ''
    def prefix = task.ext.prefix ?: "${meta.id}"
    """
    touch ${prefix}.fcs_gx_report.txt
    touch ${prefix}.taxonomy.rpt
    touch ${prefix}.summary.txt
    echo "" | gzip > ${prefix}.hits.tsv.gz

    cat <<-END_VERSIONS > versions.yml
    "${task.process}":
        fcsgx: \$( gx --help | sed '/build/!d; s/.*:v//; s/-.*//' )
    END_VERSIONS
    """
}
